import * as c from "@/util/c";
import Error404 from "@/view/error/Error404";
import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import App from "@/App";

// import fs from 'fs'
// import path from 'path'


export const routes: any[] = [];
/**
 * key: path
 * value: element
 */
export const pathElementMap = {};


function pushRoute(route: string, element, key: string) {
    routes.push(
        <Route
            path={route}
            element={element}
            key={key}
        />
    );
    pathElementMap[route] = element;

    // fs.readFile(path.join(__dirname, "./test.txt"), function (error, result) {})

}

//router 6带来的变化: https://blog.csdn.net/weixin_40906515/article/details/104957712
export function init() {
    // const context = import.meta.globEager("../view/**/*.tsx");
    // pushRoute("/", <App />, "router-App");
    // let view = Object.keys(context);
    // for (let key of view) {
    //     let View = context[key].default; //View的首字母必须大写，不能命名为view
    //     let routePath = key.replace(/(\.\.\/view\/|\.tsx)/g, "");
    //     pushRoute("/" + routePath, <View />, "router-" + routePath);
    //     if (routePath.endsWith('/index') || routePath.endsWith('/Index')) {
    //         let _routePath = routePath.substring(0, routePath.length - 5);
    //         pushRoute("/" + _routePath, <View />, "router-" + _routePath);
    //     }
    // }
    // return routes;
    pushRoute("/", <App />, "router-App");

    initExt('tsx')
    initExt('mdx')
    return routes;
}

export function initExt(ext) {
    let context = null;//import.meta.globEager("../view/**/*.tsx");
    switch (ext) {
        case 'tsx':
            context = import.meta.globEager("./view/**/*.tsx");
            break;
        case 'mdx':
            context = import.meta.globEager("./view/**/*.mdx");
            break;
        default:
            break;
    }
    let view = Object.keys(context);
    for (let key of view) {
        let View = context[key].default; //View的首字母必须大写，不能命名为view
        let routePath = null;//key.replace(/(\.\.\/view\/|\.tsx)/g, "");
        switch (ext) {
            case 'tsx':
                routePath = key.replace(/(\.\/view\/|\.tsx)/g, "");
                break;
            case 'mdx':
                routePath = key.replace(/(\.\/view\/|\.mdx)/g, "");
                break;
            default:
                break;
        }
        pushRoute("/" + routePath, <View />, "router-" + routePath);
        if (routePath.endsWith('/index') || routePath.endsWith('/Index')) {
            let _routePath = routePath.substring(0, routePath.length - 5);
            pushRoute("/" + _routePath, <View />, "router-" + _routePath);
        }
    }
    return routes;
}

export default function AppRouter(props) {
    return (
        <>
            <Router basename={c.base}>
                <Routes>
                    {init()}
                    {/* //如果都没匹配到，则报个404，注意这个一定要放最后，否则正常的页面也会报404 */}
                    <Route
                        // path={location.pathname}
                        path="*"
                        element={<Error404 location={location}></Error404>}
                        key={"router-" + "error404"}
                    />
                </Routes>
            </Router>
            {props.children}
        </>
    );
}
